<!DOCTYPE html>
<html lang="zh-CN" dir="ltr">
    <head><meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'><meta name='description' content='grep 参数 -A&amp;lt;显示行数&amp;gt;: 除了显示符合范本样式的那一列之外，并显示该行之后的内容。 -B&amp;lt;显示行数&amp;gt;: 除了显示符合样式的'>
<title>Grep</title>

<link rel='canonical' href='/p/grep/'>

<link rel="stylesheet" href="/scss/style.min.56b4b4efdee21dd7cd9a6ec6cdb1882f71985765df9c7591a90716c58b48f4c5.css"><meta property='og:title' content='Grep'>
<meta property='og:description' content='grep 参数 -A&amp;lt;显示行数&amp;gt;: 除了显示符合范本样式的那一列之外，并显示该行之后的内容。 -B&amp;lt;显示行数&amp;gt;: 除了显示符合样式的'>
<meta property='og:url' content='/p/grep/'>
<meta property='og:site_name' content='良宏'>
<meta property='og:type' content='article'><meta property='article:section' content='Post' /><meta property='article:tag' content='grep' /><meta property='article:tag' content='三剑客' /><meta property='article:published_time' content='2021-12-24T15:56:15&#43;08:00'/><meta property='article:modified_time' content='2021-12-24T15:56:15&#43;08:00'/>
<meta name="twitter:title" content="Grep">
<meta name="twitter:description" content="grep 参数 -A&amp;lt;显示行数&amp;gt;: 除了显示符合范本样式的那一列之外，并显示该行之后的内容。 -B&amp;lt;显示行数&amp;gt;: 除了显示符合样式的">
    </head>
    <body class="
    article-page
    ">
    <script>
        (function() {
            const colorSchemeKey = 'StackColorScheme';
            if(!localStorage.getItem(colorSchemeKey)){
                localStorage.setItem(colorSchemeKey, "auto");
            }
        })();
    </script><script>
    (function() {
        const colorSchemeKey = 'StackColorScheme';
        const colorSchemeItem = localStorage.getItem(colorSchemeKey);
        const supportDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches === true;

        if (colorSchemeItem == 'dark' || colorSchemeItem === 'auto' && supportDarkMode) {
            

            document.documentElement.dataset.scheme = 'dark';
        } else {
            document.documentElement.dataset.scheme = 'light';
        }
    })();
</script>
<div class="container main-container flex on-phone--column extended"><aside class="sidebar left-sidebar sticky ">
    <button class="hamburger hamburger--spin" type="button" id="toggle-menu" aria-label="切换菜单">
        <span class="hamburger-box">
            <span class="hamburger-inner"></span>
        </span>
    </button>

    <header>
        
            
            <figure class="site-avatar">
                <a href="/">
                
                    
                    
                    
                        
                        <img src="/img/wx_150x150_hub08581134c092243b209205c3073808c_6075_300x0_resize_q75_box.jpeg" width="300"
                            height="300" class="site-logo" loading="lazy" alt="Avatar">
                    
                
                </a>
                
                    <span class="emoji">🍥</span>
                
            </figure>
            
        
        
        <div class="site-meta">
            <h1 class="site-name"><a href="/">良宏</a></h1>
            <h2 class="site-description">好好学习，天天向上。</h2>
        </div>
    </header><ol class="menu" id="main-menu">
        
        
        
        <li >
            <a href='/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-home" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <polyline points="5 12 3 12 12 3 21 12 19 12" />
  <path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7" />
  <path d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6" />
</svg>



                
                <span>Home</span>
            </a>
        </li>
        
        
        <li >
            <a href='/about/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-user" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="7" r="4" />
  <path d="M6 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2" />
</svg>



                
                <span>About</span>
            </a>
        </li>
        
        
        <li >
            <a href='/archives/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-archive" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <rect x="3" y="4" width="18" height="4" rx="2" />
  <path d="M5 8v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-10" />
  <line x1="10" y1="12" x2="14" y2="12" />
</svg>



                
                <span>Archives</span>
            </a>
        </li>
        
        
        <li >
            <a href='/search/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-search" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="10" cy="10" r="7" />
  <line x1="21" y1="21" x2="15" y2="15" />
</svg>



                
                <span>Search</span>
            </a>
        </li>
        

        <div class="menu-bottom-section">
            
            
                <li id="dark-mode-toggle">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-toggle-left" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="8" cy="12" r="2" />
  <rect x="2" y="6" width="20" height="12" rx="6" />
</svg>



                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-toggle-right" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="16" cy="12" r="2" />
  <rect x="2" y="6" width="20" height="12" rx="6" />
</svg>



                    <span>暗色模式</span>
                </li>
            
        </div>
    </ol>
</aside>

    

            <main class="main full-width">
    <article class="main-article">
    <header class="article-header">

    <div class="article-details">
    
    <header class="article-category">
        
            <a href="/categories/tech/" style="background-color: blueviolet; color: #fff;">
                Tech
            </a>
        
            <a href="/categories/software/" style="background-color: #6b69d6; color: #fff;">
                Software
            </a>
        
            <a href="/categories/command/" style="background-color: brown; color: #fff;">
                Command
            </a>
        
            <a href="/categories/tool/" style="background-color: #0177b8; color: #fff;">
                Tool
            </a>
        
    </header>
    

    <div class="article-title-wrapper">
        <h2 class="article-title">
            <a href="/p/grep/">Grep</a>
        </h2>
    
        
    </div>

    
    
    
    
    <footer class="article-time">
        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-calendar-time" width="56" height="56" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <path d="M11.795 21h-6.795a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v4" />
  <circle cx="18" cy="18" r="4" />
  <path d="M15 3v4" />
  <path d="M7 3v4" />
  <path d="M3 11h16" />
  <path d="M18 16.496v1.504l1 1" />
</svg>
                <time class="article-time--published">Dec 24, 2021</time>
            </div>
        

        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-clock" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <polyline points="12 7 12 12 15 15" />
</svg>



                <time class="article-time--reading">
                    阅读时长: 5 分钟
                </time>
            </div>
        
    </footer>
    

    
</div>

</header>

    <section class="article-content">
    
    
    <h1 id="grep">grep</h1>
<h2 id="参数">参数</h2>
<pre tabindex="0"><code> -A&lt;显示行数&gt;: 除了显示符合范本样式的那一列之外，并显示该行之后的内容。
 -B&lt;显示行数&gt;: 除了显示符合样式的那一行之外，并显示该行之前的内容。
 -C&lt;显示行数&gt;: 除了显示符合样式的那一行之外，并显示该行之前后的内容。
 -c: 统计匹配的行数
 -e: 实现多个选项间的逻辑or 关系
 -E: 扩展的正则表达式
 -f FILE: 从FILE获取PATTERN匹配
 -F: 相当于fgrep
 -i --ignore-case #忽略字符大小写的差别。
 -n: 显示匹配的行号
 -n: 搜索结果显示行号
 -o: 仅显示匹配到的字符串
 -q: 静默模式，不输出任何信息
 -r: 递归查找
 -s: 不显示错误信息。
 -v: 显示不被pattern 匹配到的行，相当于[^] 反向匹配
 -w: 匹配 整个单词
</code></pre><h2 id="常用法">常用法</h2>
<pre tabindex="0"><code>查找不区分大小写
grep -rni memcpy .
在搜索结果中用反向匹配”-v“排除*.o.cmd文件的匹配
grep -rn memcpy | grep -v .o.cmd
使用多个-v依次对上一次的结果进行反向匹配
grep -rn memcpy | grep -v .o.cmd | grep -v .s.cmd | grep -v .o | grep -v .map
使用-Ev一次进行多个反向匹配搜索
grep -rn memcpy | grep -Ev &#39;\.o\.cmd|\.s\.cmd|\.o|\.map&#39;
使用”--exclude=GLOB“来指定排除某些格式的文件，如不在“*.cmd”，“*.o”和“*.map”中搜索
grep -rn --exclude=*.cmd --exclude=*.o --exclude=*.map memcpy
跟“--exclude=GLOB”类似的用法有“--include=GLOB”，从指定的文件中搜索，如只在“*.cmd”，“*.o”和“*.map”中搜索
grep -rn --include=*.cmd --include=*.o --include=*.map memcpy
“--include=GLOB”在不确定某些函数是否被编译时特别有用
grep -rn --include=*.o rpi_is_serial_active
不在某些指定的目录查找
grep -rn --exclude-dir=out memcpy
忽略多个目录
grep -rn --exclude-dir=out --exclude-dir=doc memcpy
查找精确匹配结果
grep -rnw memcpy .
查找作为单词分界的结果
grep -rn -E &#34;(\b|_)memcpy(\b|_)&#34;
查看查找结果的上下文想看宏定义“MEMCPY”匹配的前三行和后两行
grep -rn -B 3 -A 2 MEMCPY

find命令先找出makefile类文件，然后再从结果中搜索CFLAGS
find . -iname Makefile -o -iname *.inc -o -iname *.mk | xargs grep -rn CFLAGS
grep -rn --include=Makefile --include=*.inc --include=*.mk CFLAGS .

在linux目录中查找所有的*.h，并在这些文件中查找“SYSCALL_VECTOR”
find linux -name *.h | xargs grep &#34;SYSCALL_VECTOR&#34;
打印出所有包含”SYSCALL_VECTOR”字符串的文件名
find linux -name *.h | xargs grep -l &#34;SYSCALL_VECTOR&#34;
grep的选项-l只打印匹配的文件名
</code></pre><p>实战脚本</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="cp">#!/usr/bin/env bash
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="c1">#echo &lt;&lt;&lt;EOT &gt; test.txt</span>
</span></span><span class="line"><span class="cl"><span class="c1">#aaaa</span>
</span></span><span class="line"><span class="cl"><span class="c1">#bbbbbb</span>
</span></span><span class="line"><span class="cl"><span class="c1">#AAAaaa</span>
</span></span><span class="line"><span class="cl"><span class="c1">#BBBBBASDABBDA</span>
</span></span><span class="line"><span class="cl"><span class="c1">#EOT</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug<span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">  <span class="nb">set</span> +x
</span></span><span class="line"><span class="cl">  sleep <span class="m">1</span>
</span></span><span class="line"><span class="cl">  <span class="nb">set</span> -x
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">tee test.txt &gt; /dev/null <span class="s">&lt;&lt;EOT
</span></span></span><span class="line"><span class="cl"><span class="s">aaa
</span></span></span><span class="line"><span class="cl"><span class="s">bbbbbb
</span></span></span><span class="line"><span class="cl"><span class="s">AAAaaa
</span></span></span><span class="line"><span class="cl"><span class="s">BBBBBASDABBDA
</span></span></span><span class="line"><span class="cl"><span class="s">EOT</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;aaa&#34;</span> &gt; grep.txt
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># cat grep.txt</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">grep -A2 b test.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep -B1 b test.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep -C1 b test.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep -c aaa test.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep -e AAA -e bbb test.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep -in b test.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep -o ASDA test.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep -q aa test.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep -v aaa test.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep -w aaa test.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep -f grep.txt test.txt
</span></span></code></pre></div><h2 id="正则表达式">正则表达式</h2>
<pre tabindex="0"><code> . 匹配任意单个字符，不能匹配空行
  [] 匹配指定范围内的任意单个字符
  [^] 取反
  [:alnum:] 或 [0-9a-zA-Z]
  [:alpha:] 或 [a-zA-Z]
  [:upper:] 或 [A-Z]
  [:lower:] 或 [a-z]
  [:blank:] 空白字符（空格和制表符）
  [:space:] 水平和垂直的空白字符（比[:blank:]包含的范围广）
  [:cntrl:] 不可打印的控制字符（退格、删除、警铃...）
  [:digit:] 十进制数字 或[0-9]
  [:xdigit:]十六进制数字
  [:graph:] 可打印的非空白字符
  [:print:] 可打印字符
  [:punct:] 标点符号
</code></pre><p>实战</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="cp">#!/usr/bin/env bash
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>------------------------sleepAndExecDebug<span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">  <span class="nb">set</span> +x
</span></span><span class="line"><span class="cl">  sleep <span class="m">1</span>
</span></span><span class="line"><span class="cl">  <span class="nb">set</span> -x
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;aaa&#34;</span> &gt; grep.txt
</span></span><span class="line"><span class="cl">tee test.reg.txt &gt; /dev/null <span class="s">&lt;&lt;EOT
</span></span></span><span class="line"><span class="cl"><span class="s">abc
</span></span></span><span class="line"><span class="cl"><span class="s">123
</span></span></span><span class="line"><span class="cl"><span class="s">        
</span></span></span><span class="line"><span class="cl"><span class="s">//[
</span></span></span><span class="line"><span class="cl"><span class="s">EOT</span>
</span></span><span class="line"><span class="cl">tee test.reg2.txt &gt; /dev/null <span class="s">&lt;&lt;EOT
</span></span></span><span class="line"><span class="cl"><span class="s">ggle
</span></span></span><span class="line"><span class="cl"><span class="s">gogle
</span></span></span><span class="line"><span class="cl"><span class="s">google
</span></span></span><span class="line"><span class="cl"><span class="s">goooooooooooooooooooooooooogle
</span></span></span><span class="line"><span class="cl"><span class="s">gagle
</span></span></span><span class="line"><span class="cl"><span class="s">EOT</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">grep . test.reg.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="o">[</span>a/<span class="o">]</span> test.reg.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="o">[</span>^abc<span class="o">]</span> test.reg.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="o">[[</span>:alnum:<span class="o">]]</span> test.reg.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="o">[</span>a-z<span class="o">]</span> test.reg.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="o">[[</span>:space:<span class="o">]]</span> test.reg.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="o">[[</span>:punct:<span class="o">]]</span> test.reg.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="s2">&#34;g[o]*gle&#34;</span> test.reg2.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="s2">&#34;g[o].*gle&#34;</span> test.reg2.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="s2">&#34;g[o]\?gle&#34;</span> test.reg2.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="s2">&#34;g[o]\+gle&#34;</span> test.reg2.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="s2">&#34;g[o]\{1,2\}gle&#34;</span> test.reg2.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep -E <span class="s2">&#34;g[o]{10,}gle&#34;</span> test.reg2.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">egrep <span class="s2">&#34;g[o]{,10}gle&#34;</span> test.reg2.txt
</span></span></code></pre></div><h3 id="位置锚定">位置锚定</h3>
<pre tabindex="0"><code>  ^  行首锚定，用于模式的最左侧
  $  行尾锚定，用于模式的最右侧
  ^PATTERN$，用于模式匹配整行
  ^$ 空行
  ^[[:space:]].*$  空白行
  \&lt; 或 \b  词首锚定，用于单词模式的左侧
  \&gt; 或 \b  词尾锚定；用于单词模式的右侧
  \&lt;PATTERN\&gt;
</code></pre><p>实战</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="cp">#!/usr/bin/env bash
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>------------------------sleepAndExecDebug<span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">  <span class="nb">set</span> +x
</span></span><span class="line"><span class="cl">  sleep <span class="m">1</span>
</span></span><span class="line"><span class="cl">  <span class="nb">set</span> -x
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">tee test.reg3.txt &gt; /dev/null <span class="s">&lt;&lt;EOT
</span></span></span><span class="line"><span class="cl"><span class="s">aaa
</span></span></span><span class="line"><span class="cl"><span class="s">
</span></span></span><span class="line"><span class="cl"><span class="s">
</span></span></span><span class="line"><span class="cl"><span class="s">bbbbbb
</span></span></span><span class="line"><span class="cl"><span class="s">acdfsddsfb
</span></span></span><span class="line"><span class="cl"><span class="s">EOT</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep ^a test.reg3.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep b$ test.reg3.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep ^$ test.reg3.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep ^<span class="o">[[</span>:space:<span class="o">]]</span>.*$ test.reg3.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="s2">&#34;\&lt;a.*b\&gt;&#34;</span> test.reg3.txt
</span></span></code></pre></div><h3 id="分组和后向引用">分组和后向引用</h3>
<p>① 分组：() 将一个或多个字符捆绑在一起，当作一个整体进行处理
　　分组括号中的模式匹配到的内容会被正则表达式引擎记录于内部的变量中，这些变量的命名方式为: \1, \2, \3, &hellip;
② 后向引用
引用前面的分组括号中的模式所匹配字符，而非模式本身
\1 表示从左侧起第1个左括号以及与之匹配右括号之间的模式所匹配到的字符
\2 表示从左侧起第2个左括号以及与之匹配右括号之间的模式所匹配到的字符，以此类推
&amp; 表示前面的分组中所有字符</p>
<p><img src="/1.png"
	
	
	
	loading="lazy"
	
		alt="1.png"
	
	
></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="cp">#!/usr/bin/env bash
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>------------------------sleepAndExecDebug<span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">  <span class="nb">set</span> +x
</span></span><span class="line"><span class="cl">  sleep <span class="m">1</span>
</span></span><span class="line"><span class="cl">  <span class="nb">set</span> -x
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">tee test.reg4.txt &gt; /dev/null <span class="s">&lt;&lt;EOT
</span></span></span><span class="line"><span class="cl"><span class="s">Hello world Hello world
</span></span></span><span class="line"><span class="cl"><span class="s">Heiii world Heiii world
</span></span></span><span class="line"><span class="cl"><span class="s">Hello world Heiii wwwww
</span></span></span><span class="line"><span class="cl"><span class="s">EOT</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="s2">&#34;\(He\)&#34;</span> test.reg4.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="s2">&#34;\(He\).*\1&#34;</span> test.reg4.txt
</span></span><span class="line"><span class="cl">------------------------sleepAndExecDebug
</span></span><span class="line"><span class="cl">grep <span class="s2">&#34;\(He\).*\(wo\).*\2&#34;</span> test.reg4.txt
</span></span></code></pre></div><h3 id="扩展正则表达式">扩展正则表达式</h3>
<p>（1）字符匹配：
.  任意单个字符
[]  指定范围的字符
[^] 不在指定范围的字符
次数匹配：</p>
<ul>
<li>：匹配前面字符任意次
?  : 0 或1次</li>
</ul>
<ul>
<li>：1 次或多次
{m} ：匹配m次 次
{m,n} ：至少m ，至多n次</li>
</ul>
<p>（2）位置锚定：
^ : 行首
$ : 行尾
&lt;, \b : 语首
&gt;, \b : 语尾
分组：()
后向引用：\1, \2, &hellip;</p>
<p>（3）总结
　　除了&lt;, \b : 语首、&gt;, \b : 语尾；使用其他正则都可以去掉\；</p>
<h1 id="ack">ack</h1>
<pre tabindex="0"><code># http://betterthangrep.com/documentation/
# http://codeseekah.com/2012/03/11/ack-vs-grep/
sudo apt install ack
# 速度非常快,因为它只搜索有意义的东西。
# 更友好的搜索，忽略那些不是你源码的东西。
# 为源代码搜索而设计，用更少的击键完成任务。
# 非常轻便，移植性好。
# 免费且开源

-c(统计)/ -i(忽略大小)/ -h(不显示名称)/
-l(只显文件名)/ -n(加行号)/ -v(显示不匹配)

文本搜索
ack hello
ack -i hello  # -i, --ignore-case   屏蔽大小写
                   # -I, --no-ignore-case  不屏蔽大小写(默认)
ack -v hello  # -v, --invert-match  反向匹配
ack -w hello  # -w, --word-regexp   单词匹配
ack -Q &#39;hello*&#39;    # -Q, --literal  引用所有元字符； PATTERN 是文字

文件搜索
ack --line=1       # 输出所有文件第二行
ack -l &#39;hello&#39;     # 包含的文件名
ack -L &#39;print&#39;     # 非包含文件名


ack hello --pager=&#39;less -R&#39;    # 以less形式展示
ack hello --noheading      # 不在头上显示文件
ack hello --nocolor        # 不对匹配字符着色


ack -f hello.py    # 查找全匹配文件
ack -g hello.py$    # 查找正则匹配文件
ack -g hello  --sort-files     #查找然后排序


ack --python hello #查找所有python文件
ack -G hello.py$ hello     # 查找匹配正则的文件

ack配置
# 设置排序
--sort-files
#设置文件过滤
--python
--html
--js
--conf
# 设置显示
--noheading
# 定义新的文件类型
--type-set=conf=.conf
# 智能识别大小写
--smart-case
# 设置以less形式展示，设定less参数
--pager=less -R -M --shift 5 -i
</code></pre><h1 id="ag">ag</h1>
<pre tabindex="0"><code>sudo apt-get install silversearcher-ag

ag main
ag &#34;main(&#34;
ag &#34;main\(&#34; src
ag &#34;main\(&#34; --ignore-dir 3rdparty
ag &#34;main\(&#34; --ignore-dir 3rdparty --ignore-dir doc
ag &#34;http&#34; -l | xargs sed -i &#39;s/http/https/g&#39;
grep -ri &#34;http&#34; * -l | xargs sed -i &#39;s/http/https/g&#39;

ag 另外其他很有用的選項是
ag -i 使用大小寫不敏感的匹配方式
ag -w 全詞匹配
ag -G &#34;.+\.java&#34; 搜索 java 類型的檔案
ag -l 顯示有匹配的檔案路徑
ag -L 顯示沒有任何匹配的檔案路徑
ag -v 反向匹配，將匹配到的資料排除

ag 搭配正規表達式
ag ^abc log.txt
ag abc$ log.txt
ag abc[0-9] log.txt
</code></pre>
</section>


    <footer class="article-footer">
    
    <section class="article-tags">
        
            <a href="/tags/grep/">grep</a>
        
            <a href="/tags/%E4%B8%89%E5%89%91%E5%AE%A2/">三剑客</a>
        
    </section>


    </footer>


    
</article>

    

    

<aside class="related-content--wrapper">
    <h2 class="section-title">相关文章</h2>
    <div class="related-content">
        <div class="flex article-list--tile">
            
                
<article class="">
    <a href="/p/go-callvis/">
        
        

        <div class="article-details">
            <h2 class="article-title">go-callvis</h2>
        </div>
    </a>
</article>

            
                
<article class="">
    <a href="/p/netstat/">
        
        

        <div class="article-details">
            <h2 class="article-title">Netstat</h2>
        </div>
    </a>
</article>

            
                
<article class="">
    <a href="/p/find/">
        
        

        <div class="article-details">
            <h2 class="article-title">Find</h2>
        </div>
    </a>
</article>

            
                
<article class="">
    <a href="/p/awk/">
        
        

        <div class="article-details">
            <h2 class="article-title">Awk</h2>
        </div>
    </a>
</article>

            
                
<article class="">
    <a href="/p/sed/">
        
        

        <div class="article-details">
            <h2 class="article-title">sed</h2>
        </div>
    </a>
</article>

            
        </div>
    </div>
</aside>

     
    
        
    

    <footer class="site-footer">
    <section class="copyright">
        &copy; 
        
            2021 - 
        
        2023 良宏
    </section>
    <section class="powerby">
        Built with <a href="https://gohugo.io/" target="_blank" rel="noopener">Hugo</a>,
        主题 <b><a href="https://github.com/CaiJimmy/hugo-theme-stack" target="_blank" rel="noopener" data-version="3.5.0">Stack</a></b> 由 <a href="https://jimmycai.com" target="_blank" rel="noopener">Jimmy</a> 设计
    </section>
</footer>


    
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    
    <div class="pswp__bg"></div>

    
    <div class="pswp__scroll-wrap">

        
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                
                
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"integrity="sha256-ePwmChbbvXbsO02lbM3HoHbSHTHFAeChekF1xKJdleo="crossorigin="anonymous"
                defer
                >
            </script><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"integrity="sha256-UKkzOn/w1mBxRmLLGrSeyB4e1xbrp4xylgAWb3M42pU="crossorigin="anonymous"
                defer
                >
            </script><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css"crossorigin="anonymous"
            ><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css"crossorigin="anonymous"
            >

            </main>
        </div>
        <script 
                src="https://cdn.jsdelivr.net/npm/node-vibrant@3.1.6/dist/vibrant.min.js"integrity="sha256-awcR2jno4kI5X0zL8ex0vi2z&#43;KMkF24hUW8WePSA9HM="crossorigin="anonymous"
                
                >
            </script><script type="text/javascript" src="/ts/main.js" defer></script><script type="text/javascript" src="/ts/custom.js" defer></script>
<script>
    (function () {
        const customFont = document.createElement('link');
        customFont.href = "https://fonts.font.im/css2?family=Lato:wght@300;400;700&display=swap";

        customFont.type = "text/css";
        customFont.rel = "stylesheet";

        
    }());
</script>


    </body>
</html>
